﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Net.Mime;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevelopmentPFR.Utilitaire;
using DevelopmentPFR.Classes;

namespace DevelopmentPFR.Centrale_Achat
{
    public partial class CommandesHypermarches : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ListeCommande.DataSource = BaseDeDonnees.ExecuteSelectStoredProcedure("spCommun_ListeCommande");
                ListeCommande.DataBind();

                foreach (GridViewRow row in ListeCommande.Rows)
                {
                    ((HyperLink)row.FindControl("linkToDetail")).NavigateUrl = "DetailCommandesHypermarches.aspx?ref=" + ((HyperLink)row.FindControl("linkToDetail")).Text;
                }
            }
        }

        protected void genererCommandeInterne_Click(object sender, EventArgs e)
        {
            string listeReference = "";
            foreach (GridViewRow row in ListeCommande.Rows)
            {
                var checkProduit = row.FindControl("commandeCheck") as CheckBox;
                if (checkProduit != null && checkProduit.Checked)
                {
                    listeReference += ((HyperLink) row.FindControl("linkToDetail")).Text + ",";
                }
            }
            if (listeReference.Length != 0)
            {
                listeReference = listeReference.Remove(listeReference.Length - 1);
                if (BaseDeDonnees.ExecuteUpdateStoredProcedure("spCa_createCommandeCA", new Hashtable()
                {
                    {"@referenceHyp", listeReference}
                }))
                {
                    Response.Redirect("CommandesFournisseurs.aspx");    
                }
            }
        }

        protected void CheckCommmandeOldDate(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //Selection des lignes dont la quantité est inférieur au stock min
                if ((DateTime.Now - Convert.ToDateTime(e.Row.Cells[1].Text)).TotalDays > 3)
                {
                    e.Row.BackColor = Color.Red;
                    var checkCommande = e.Row.FindControl("commandeCheck") as CheckBox;
                    checkCommande.Checked = true;
                }
            }
        }
    }
}